package com.demo.crud.controller;

import com.alibaba.druid.util.HttpClientUtils;
import com.demo.crud.annotations.NotRepeatSubmit;
import com.demo.crud.dao.UserDao;
import com.demo.crud.dao.UserDao2;
import com.demo.crud.entities.User;
import com.demo.crud.service.TestService;
import com.demo.crud.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.sql.DataSource;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: keen
 * Date: 2019-06-05
 * Time: 22:18
 */
@RestController
public class HelloController {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserDao2 userDao2;

    @Autowired
    private TestService testService;

    @NotRepeatSubmit
    @GetMapping("/hello")
    public String hello() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello";
    }

    @GetMapping("/getToken")
    public String getToken() {
        String token = TokenUtils.generateToken();
        System.out.println(token);
        return token;
    }

    @NotRepeatSubmit
    public String save() {
        return "保存成功!";
    }

    @GetMapping("/dataSource")
    public String dataSource() {
        Class<? extends DataSource> dataSourceClass = dataSource.getClass();
        return dataSourceClass.toString();
    }

    @ResponseBody
    @GetMapping("/getUsers")
    public List<Map<String, Object>> getUsers() {
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from t_user");
        return mapList;
    }

    @ResponseBody
    @GetMapping("/getUsers2")
    public List<User> getUsers2() {
        List<User> users = userDao2.getUsers();
        users.addAll(userDao.getUsers());
        return users;
    }

    @GetMapping("/testThreadTransaction")
    public void testThreadTransaction() throws InterruptedException {
       testService.test2();
    }
}
